package com.example.gamecenter.activitys

import android.graphics.Color
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.gamecenter.R
import com.example.gamecenter.fragments.detail.DetailFragment1
import com.example.gamecenter.fragments.detail.DetailFragment2
import com.example.gamecenter.fragments.detail.DetailFragment3
import com.google.android.material.badge.BadgeDrawable
import com.google.android.material.tabs.TabLayoutMediator
import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.activity_detail.iv_header_bg


class DetailActivity: AppCompatActivity() {
    private val context = this;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        // 创建一个适配器实例设置为ViewPager2对象
        val adapter = MyFragmentStateAdapter(context)
        val viewPager2 = findViewById<ViewPager2>(R.id.detail_view_pager)
        viewPager2.adapter = adapter

        // https://appimg-drcn.dbankcdn.com/application/screenshut1/c2ac1172d63e42b89227e127f916ff9f_13.jpg
        //  顶部背景
        val header_bg = findViewById<ImageView>(R.id.iv_header_bg)
        Glide.with(context)
            .load("https://appimg-drcn.dbankcdn.com/application/screenshut1/c2ac1172d63e42b89227e127f916ff9f_13.jpg")
            .apply(RequestOptions().transform(CenterCrop(), RoundedCorners(30)))
            .into(header_bg);

        // 使用TabLayoutMediator将TabLayout和ViewPager2组合起来
        // 在TabLayout中添加Tab
        val tabLayout = findViewById<TabLayout>(R.id.tabs_detail)
        for (i in 0 until adapter.getItemCount()) {
            tabLayout.addTab(tabLayout.newTab())
        }
        val mediator = TabLayoutMediator(
            tabLayout, viewPager2
        ) { tab, position -> // 设置Tab的文本
            tab.setText(adapter.getPageTitle(position))
            if (position == 1 || position == 2) {
                tab.orCreateBadge.apply {
                    backgroundColor = Color.TRANSPARENT;
                    badgeTextColor = Color.RED;
                    number = 999;
                    badgeGravity = BadgeDrawable.TOP_END
                }
            }
        }
        mediator.attach()
    }
}

// 自定义FragmentStateAdapter
class MyFragmentStateAdapter(fa: FragmentActivity?) : FragmentStateAdapter(fa!!) {
    override fun createFragment(position: Int): Fragment {
        // 返回不同位置的Fragment
        return when (position) {
            0 -> DetailFragment1()
            1 -> DetailFragment2()
            2 -> DetailFragment3()
            else -> throw error("not found fragment")
        }
    }
    override fun getItemCount(): Int {
        return NUM_PAGES
    }

    companion object {
        private const val NUM_PAGES = 3
    }
    fun getPageTitle(position: Int): String{
        return when (position) {
            0 ->"介绍"
            1 ->"评论"
            2 ->"论坛1"
            else -> throw error("not found fragment")
        }
    }
}